import { Component } from '@angular/core';
import { NavController,AlertController } from 'ionic-angular';
import { HTTP } from '@ionic-native/http';

import { ZhucePage } from "../zhuce/zhuce";
import { FortPage } from "../fortpass/fortpass";
// import { HomePage } from "../home/home";
import { TabsPage } from '../tabs/tabs';



@Component({
  selector: 'page-index',
  templateUrl: 'index.html'
})
export class IndexPage {
    public static usename:string="";
    public static usepassword:string='';
    userName:string;  //账户
    userPassword:string; //密码
     public static userid:string="";  //id
    public static username:string=''; //用户名
    public static usermoney:string="0"; //用户金钱

   public url:string ="http://118.31.79.20:8080/";//接口地址
    public loginurl:string = this.url + "Stock/login/login.do";//登录接口
  
  constructor(public navCtrl: NavController,private http: HTTP,private malert:AlertController) {
            
  }

    
  login(){
        //正则验证手机号
        var iphone = /^0?1[3|4|5|8][0-9]\d{8}$/;
       
       
        if(this.userName == '' || this.userName == null || this.userPassword ==''||this.userPassword == null){
           
            this.mWinds("您好，请输入账号名和密码!!");
            return false;
        }else if(!iphone.test(this.userName)){
          
              this.mWinds("您好，账户格式不正确!!");
            return false;
        }else{
          this.http.post(this.loginurl,{"username":this.userName,"password":this.userPassword},{})   //接口。发送的数据
              .then(data => {
                  // console.log("请求成功");
                 let datas=JSON.parse(data.data); 
                IndexPage.usename = this.userName;
                IndexPage.usepassword = this.userPassword;
                 
                if(datas.flag == true){    //成功跳转等页面
                    
                    this.saveData();
                }else{
                  
                      this.mWinds("您好，账户或密码不正确！");
                    return false;
                }
                
              }).catch(error => {
                  console.log("请求失败");
                  console.log(error);
              
              });
        }
    }

  zhu(){
      this.navCtrl.push(ZhucePage);
  }
  // wang(){
  //   this.navCtrl.push(FortPage);
  // }
    saveData(){
         let logisurl:string ="http://118.31.79.20:8080/Stock/login/login.do?username="+IndexPage.usename+'&&password='+ this.userPassword; 
         // 个人信息请求
            this.http.get(logisurl, {}, {})
             .then(data => {
                //获取的数据
                var datass=JSON.parse(data.data);
                IndexPage.userid=datass.data.uid; //用户id
                IndexPage.username = datass.data.username; //用户名
                IndexPage.usermoney = datass.data.money; //用户牛牛币

                localStorage.setItem("userid",IndexPage.userid);
                localStorage.setItem("username",IndexPage.username);
                localStorage.setItem("userpassword",IndexPage.usepassword);
                //数据改变
                // alert(IndexPage.userid);
                console.log('登录id'+localStorage.getItem("userid"))
                console.log('登录账户'+localStorage.getItem("username"))
                console.log('登录密码'+localStorage.getItem("userpassword"))
                 this.navCtrl.push(TabsPage);
                
            })
            .catch(error => {
                 
                  this.mWinds("网络异常");
                 
            });
    }      
       /**
    * 自定义弹框
    */
    mWind(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,
      buttons:[{
        text:'取消',
        handler:()=>{         
        }
      },{
        text:'确定',
        handler:()=>{       
        }
      }],
      
    });
  alert.present();

    }
    /**
     * 无按钮
     * @param msg 
     */
    mWinds(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,     
    });
  alert.present();

    }
         
  
}
